package com.zj.base;

/**
 * @author zhangbo
 * @program: algorithm
 * @description: $ 二分法
 * @date 2022-12-19 16:58:53
 */
public class Dichotomy {


    /**
     * 1 2 3 4 5
     * 思路
     * 有序，从中间取
     *
     * @param arr
     */
    public int sort(int[] arr, int num) {
        int l = 0;
        int r = arr.length;


        while (l < r) {
            int mid = ((r - l) >> 1) + l;

            if (arr[mid] == num) {
                return mid;
            }

            if (arr[mid] > num) {
                l = mid;
            } else {
                r = mid + 1;
            }


        }

        return -1;

    }
}
